package com.scisoft.scan.ws;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scisoft.scan.data.check.CheckDetailBean;
import com.scisoft.scan.data.check.CheckOrderBean;
import com.scisoft.scan.data.delivery.DeliveryBean;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.HeaderProperty;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class WebServiceHelper_Check {

    public static  String GetWS_Url(){
        return String.format("http://%s/dymps/ws/SciCheckServer?wsdl", SysParameter.WS_IPandPort);
    }

    public  static  String WS_Method = "receive";

    public  static  String WS_Namespace = "http://check.scan.ws.dms.com/";

    //查询盘点当 (bizId 1 未盘 2 在盘 3 已审)
    public static List<CheckOrderBean> CHECK_01(String bizId) throws Exception {
//        List<CheckOrderBean> ret = new ArrayList<>();
//        CheckOrderBean c = new CheckOrderBean();
//        c.orderNo = "AAA";
//        c.allCount = "100";
//        c.depotName = "J仓";
//        c.reservoirArea = "J01区";
//        c.orderId = "1";
//        c.depotId= "3";
//        c.depotNo= "4";
//        c.typeCount= "5";
//        c.checkCount= "6";
//        c.operator= "7";
//        c.optime= "8";
//        ret.add(c);
//        return ret;
        //以下代码可以复用
        String arg0 = String.format( "{\"flowNo\":\"CHECK_01\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        List<CheckOrderBean> ret = new ArrayList<>();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret= gson.fromJson(jsonArray.toString(),new TypeToken<List<CheckOrderBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询盘点单明细(bizId 盘点单ID)
    public static List<CheckDetailBean> CHECK_02(String bizId) throws Exception {

//        List<CheckDetailBean> ret = new ArrayList<>();
//        CheckDetailBean c = new CheckDetailBean();
//        c.depotId = "J";
//        c.depotName = "J仓";
//        c.areaId = "J01";
//        c.areaName = "J仓A区";
//        c.position = "J01-001";
//        c.partName = "车窗";
//        c.allCount = "100";
//        c.occupyCount = "25";
//        c.manufacturerName = "大运";
//        c.dtlId="1";
//        c.partId="2";
//        c.partCode="3";
//        c.manufacturer="4";
//        c.depotNo="6";
//        c.state="3";
//        c.checkCount="8";
//        c.imgUrl="";
//        ret.add(c);
//        return ret;
        //以下代码可以复用
        String arg0 = String.format( "{\"flowNo\":\"CHECK_02\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        List<CheckDetailBean> ret = new ArrayList<>();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret= gson.fromJson(jsonArray.toString(),new TypeToken<List<CheckDetailBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //盘点上传 每次点击某个配件，输入盘点数量后，应该进行上传
    public static String CHECK_03(CheckDetailBean checkDetailBean)throws Exception {

//        return "成功";
        //以下代码可以复用
        Gson gson = new Gson();
        String parm =gson.toJson(checkDetailBean);
        String arg0 = String.format("{\"flowNo\":\"CHECK_03\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\"}",SysParameter.user,parm, SysParameter.ScSequence);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret="成功";
        if (msg!=null&&msg.contains("成功"))
        {

        }else
        {
            ret =msg;
            throw new Exception(msg);
        }
        return ret;
     }

    //审核 盘点单ID
    public static String CHECK_04(String bizId)throws Exception {

//        return "成功";
        //以下代码可以复用
        String arg0 = String.format( "{\"flowNo\":\"CHECK_04\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user,SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret="成功";
        if (msg!=null&&msg.contains("成功"))
        {

        }else
        {
            ret =msg;
            throw new Exception(msg);
        }
        return ret;
    }

    public static  SoapObject CallWebservice(String arg0) throws Exception {
        //实例化SoapObject对象：命名空间+方法名称
        SoapObject request = new SoapObject(WS_Namespace, WS_Method);
        //设置WebService接口需要传入的参数
        request.addProperty("arg0", arg0);

        //创建SoapSerializationEnvelope 对象，同时指定soap版本号
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
        envelope.dotNet = false;//由于是.net开发的webservice，所以这里要设置为true

        //调用接口
        ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
        headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
        HttpTransportSE httpTransportSE = new HttpTransportSE(GetWS_Url());
        httpTransportSE.call(null, envelope,headerPropertyArrayList);//调用

        // 获取返回的数据
        SoapObject result = (SoapObject) envelope.bodyIn;
        return result;
    }
}
